Repository navigation
feat(pdf-codec): a document-wide decode budget (maxDecodedBytes) - #1815
Merged
Merged
Conversation
Mearman
marked this pull request as ready for review
November 16, 2026 11:37
Mearman
force-pushed
the
feat/pdf-codec-decode-budget
branch
from
November 24, 2026 21:08
ffeadca to
11c943f
Compare
|
If your organization's extra usage balance is empty or was never set up, an Owner of your Claude organization can add extra usage credits at claude.ai/admin-settings/usage. If your organization's monthly spend limit for extra usage is $0 or was reached, an Owner can raise it on the same page. If neither applies, contact Anthropic support. Once that's fixed, PR Steward starts at the next push to this pull request. |
readPdf, renderPdfPage and openPdfDocument take maxDecodedBytes, one budget for every decode the document reaches: cross-reference and object streams (recovery included), content streams, form XObjects, font programs, ToUnicode and CIDToGIDMap streams, images, inline images, soft masks, Indexed lookups, metadata and embedded files. Every reader now decodes through the resolver, so no site can decode outside it. The first decode that would pass the budget throws PdfDecodeBudgetError, a PdfParseError carrying the budget, the bytes already spent, the stream's object number and its filters. Flate inflates in slices sized to what the remainder can absorb, LZW, RunLength and ASCII85 stop at the first byte past it, and the whole-image decoders (CCITTFax, JBIG2, JPEG 2000, a rasterised JPEG) are charged their declared output before they decode. decodeStream takes the budget directly. Without the option nothing is charged and every decoder runs its existing code.
…conversion UnifiedConversionOptions, PdfToDocumentOptions, ReadNativeDocumentTreeOptions and ReadDocumentMetadataOptions take maxDecodedBytes and hand it to readPdf, so a pdf conversion that would decode too much throws PdfDecodeBudgetError, now re-exported, rather than a generic failure. Like pages and surfaces it reaches only the hop that reads the caller's own pdf, never an intermediate pdf a route renders.
…nflate size hint under a budget openPdfImages, which landed on main after this branch started, opened its document without a budget, so a one-at-a-time image read escaped maxDecodedBytes. It now takes the option and charges listing and every decode against the one budget. The expected-length hint main added to decodeStream moves into DecodeStreamOptions beside the budget, and the per-image colour-space resolver passes decodeStream and chargeDecodedBytes through.
…gainst maxDecodedBytes
Mearman
force-pushed
the
feat/pdf-codec-decode-budget
branch
from
November 25, 2026 09:16
11c943f to
1b5a3b6
Compare
Contributor
|
🎉 This PR is included in version 5.10.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1814
readPdf,renderPdfPageandopenPdfDocumenttakemaxDecodedBytes, one budget charged by every decode the document reaches. The first decode that would pass it throwsPdfDecodeBudgetError(aPdfParseError, codepdf/decode-budget-exceeded, carryingmaxDecodedBytes,decodedBytesalready spent, the stream'sobjectNumberand itsfilters). documents.js forwards the option from every pdf conversion and read and re-exports the error.Decisions
PdfObjectResolvergainsdecodeStream(stream, sink)andchargeDecodedBytes(stream, n), and every reader decodes through it instead of importingfilters.ts'sdecodeStream. Onlydocument.tsandxref.ts(which charge the budget themselves) import it, and a test fails if any other module starts to, so a new decode site cannot bypass the budget./Contentsarrays, form XObjects, image XObjects and inline images,/SMask,/Indexedlookups,/Metadata, embedded files,/ToUnicode,/CIDToGIDMap(font read and raster), and font programs for encoding, PostScript name, digest and raster outlines.[/ASCII85Decode /FlateDecode]pays for both. A predictor is not charged again, since its output is never longer than the stage it rewrites. A stream decoded twice is charged twice (a program two fonts share costs two decodes); the existing per-stream name and digest caches still decode once.inflateTolerant's four recovery tiers. LZW, RunLength and ASCII85 throw at the first byte past the limit. ASCIIHex is refused from its exact size before allocating. CCITTFax is refused from/Rows(or, with none, the most rows its input bits can encode), JBIG2 from/Widthand/Height(refused without them), JPEG 2000 from its sample planes beforedecodeJpeg2000allocates them (newonSampleAllocationoption), and a JPEGrenderPdfPagehands to a rasteriser from its frame header.readPdfpasses JPEGs through undecoded, so they cost nothing there.readJpeg2000Imagerethrows the budget error instead of degrading to "skip this image", and once one decode is refused the budget is spent, so later decodes are refused even through a caller that caught the first error.fetch()before decoding, so the budget charges plaintext decoding. Cross-reference streams are never encrypted. An object stream found by recovery is decoded before the decryptor exists, as it already was, and is charged for whatever that produces.decodeStreamcalls the same unbounded decoders as before (inflateTolerant, not the bounded ladder), so output is unchanged.UnifiedConversionOptions,PdfToDocumentOptions,ReadNativeDocumentTreeOptionsandReadDocumentMetadataOptionstakemaxDecodedBytesand forward it likepagesandsurfaces: only to the hop that reads the caller's own pdf, never an intermediate pdf a route renders.Not bounded
Work that produces no decoded bytes: content interpretation, text layout and PNG encoding, which scale with page and content-stream sizes a caller can measure beforehand. JBIG2's intermediate bitmaps stay bounded by its existing per-bitmap ceiling rather than charged.
Tests
src/decode-budget.test.tsandsrc/decode-budget-sites.test.ts: each filter refuses a small stream expanding past the budget, and decodes what fits byte-for-byte as without one (seeded sizes, exact-fit and one-short budgets);inflateTolerantBoundedagainstinflateTolerantover 200 seeded valid, truncated, whitespace-prefixed and raw streams; one document per site whose only large stream is at that site, which reads without a budget and throws naming that object with one (reverting any site's charge makes its test fail); shared font programs charged twice; JPEG 2000 samples; encrypted object streams;renderPdfPage's content, font-program,CIDToGIDMapand JPEG charges; every existing document fixture reads identically with an unlimited budget and with none.src/convert/from-pdf-decode-budget.test.ts(documents.js): each pdf conversion and read refuses withPdfDecodeBudgetError, converts within the budget, and an intermediate pdf is not charged.test/workers/pdf-codec.test.ts: the refusal inside workerd.